<template>
  <div class="app-container" v-loading="loading">
    <el-form :inline="true" :model="queryParams">
      <div style="display: flex;flex-wrap: wrap">
        <el-col :span="4">
          <el-form-item>
            <el-input v-model="queryParams.skuName" placeholder="商品名称" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-input v-model="queryParams.skuCode" placeholder="SKU编码" clearable/>
          </el-form-item>
        </el-col>

        <el-col :span="4">

            <el-form-item>
              <el-select v-model="queryParams.warehouseId" style="width: 100%" clearable placeholder="仓库">
                <el-option
                    v-for="item in warehouseList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </el-form-item>

        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-cascader
                style="width: 100%;"
                placeholder="分类"
                v-model="queryParams.classifyIds"
                :options="skuClassifyList"
                :show-all-levels="false"
                :props="{expandTrigger:'hover',value:'classifyId',label:'title'}"
                @change="changeClassId"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-input v-model="queryParams.attrName" placeholder="属性" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-button icon="Search" type="primary" @click="getList">查询</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </div>
    </el-form>
    <div>
      <el-button  type="primary"  >导出</el-button>
    </div>
    <vxe-table
        border
        style="margin-top: 10px;"
        align="center"
        :data="tableList"
        :height="tableHeight"
        :row-config="{isCurrent: true, isHover: true}"
    >
      <vxe-column type="seq" width="80" title="序号"></vxe-column>
      <vxe-column field="skuName" width="220" title="商品名称"></vxe-column>
      <vxe-column field="stockStatus" width="110" title="库存状态">
        <template #default="{row}">
          <div v-if="row.onhandNum >0">有货</div>
          <div v-else-if="row.onhandNum===0">无货</div>
        </template>
      </vxe-column>
      <vxe-column field="skuCode" width="220" title="SKU编码"></vxe-column>
      <vxe-column field="coverUrl" width="180" title="商品图片">
        <template #default="{row}">
          <ImagePreview v-if="row.coverUrl" :width="150" :height="50" :src="row.coverUrl" ></ImagePreview>
        </template>
      </vxe-column>
      <vxe-column field="classifyName" width="120" title="分类"></vxe-column>
      <vxe-column field="attrName" width="120" title="属性"></vxe-column>
      <vxe-column field="companyName"  width="150" title="公司名称" ></vxe-column>
      <vxe-column field="warehouseName" width="120" title="仓库"></vxe-column>
      <vxe-column field="onhandNum" width="120"  title="实际在仓数量"></vxe-column>
      <vxe-column field="allocNum" width="120" title="订单占用数量"></vxe-column>
      <vxe-column field="avlNum" width="100" title="可用数量"></vxe-column>
    </vxe-table>
    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

  </div>

</template>

<script setup>
import {nextTick, onMounted, ref} from "vue";
import {areaGetFilter, getSkuClassifyFilter, getSkuLocationInventoryList, warehouseGetFilter} from "@/api/scApi";
import ImagePreview from "@/components/ImagePreview/index.vue";

const queryParams = ref({
  pageNum: 1,
  pageSize: 10
})

let total = ref(1)
let loading=ref(false)
let tableHeight = ref(0);
nextTick(() => {
  tableHeight.value = window.innerHeight - 330;
})
window.onresize = () => {
  tableHeight.value = document.body.scrollHeight - 330;
}

onMounted(()=>{
  getWarehouseFilter()
  getSkuClassifyList()
  getList()
})
let tableList = ref([ ])

function getList() {
  loading.value=true
  if(queryParams.value.classifyIds && queryParams.value.classifyIds.length>0){
    queryParams.value.classifyId=queryParams.value.classifyIds[queryParams.value.classifyIds.length-1]
  }

  let params={
    pageNum:queryParams.value.pageNum,
    pageSize:queryParams.value.pageSize,
    param:queryParams.value
  }
  getSkuLocationInventoryList(params).then(res => {
    loading.value=false
    tableList.value = res.data
    total.value = res.total
  }).finally(()=>{
    loading.value = false
  })
}
const resetQuery = () => {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
  }
  getList();
};

let warehouseList = ref([])
function getWarehouseFilter() {
  warehouseGetFilter({
    status: 0
  }).then(res => {
    if (res.code === 200) {
      warehouseList.value = res.data.map(item => {
        item.value = item.warehouseId
        item.label = item.warehouseName
        return item
      })

    }
  })
}

//获取商品分类下拉列表数据
let skuClassifyList=ref([])
function getSkuClassifyList(){
  getSkuClassifyFilter({}).then(res=>{
    if(res.code===200){
      skuClassifyList.value =res.data
    }
  })
}
</script>

<style scoped lang="scss">
.el-form-item {
  width: 90%;
}
</style>